﻿using EYangFrame.DataAccess.Global;
using EYangFramewirk.Business.MessageSystem.Interfaces;
using EYangFramework.Common.Ninject;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EYangFramework.MessageSystem
{
    public partial class frm_DealMessage : EYangFramework.CommonForm.frm_BasicForm
    {
        private  EYangFramework.Domain.Entitys.Entrys.Message CustomMessage;
        List<EYangFramework.Domain.Entitys.Entrys.Message> messages;
        public Guid ID { get; set; }
        public string[] users;
        public frm_DealMessage()
        {
            this.AllowSingtonTimer = false;
            this.AllowInstanceTimer = true;
            InitializeComponent();
        }

        private void frm_DealMessage_Load(object sender, EventArgs e)
        {
            SetTabVisible(tp_Detail, false);
            listCommunicationUsers.Items.Clear();
            if (ID != null)
            {
                using (IBLLDealMessage bLLDealMessage = NinjectProvider.NinjectCore.GetService<IBLLDealMessage>())
                {
                    CustomMessage = bLLDealMessage.GetMessageByID(ID);
                }
                users = (CustomMessage.From + "," + CustomMessage.To).Split(',');
            }
            foreach (var item in users)
            {
                if (item != GlobalInfo.CurrentUser.USERNAME)
                {
                    listCommunicationUsers.Items.Add(item);
                }
            }
        }

        public override void ExecuteInstanceTimer(object sender, EventArgs args)
        {
            //base.ExecuteInstanceTimer(sender, args);
            BindMessage();
        }

        private void BindMessage()
        {
            using (IBLLDealMessage bLLDealMessage = NinjectProvider.NinjectCore.GetService<IBLLDealMessage>())
            {
                messages = bLLDealMessage.GetMessages(users);
                StringBuilder sb = new StringBuilder();
                foreach (var item in messages)
                {
                    sb.Append(item.From + ":\n");
                    sb.Append(item.MessageContent + "\n");
                }
                richMessage.Text = sb.ToString();
            }
        }

        private void btnDeal_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(richDealMessage.Text))
            {
                EYangFramework.Domain.Entitys.Entrys.Message currentMessage = new Domain.Entitys.Entrys.Message();
                currentMessage.From = GlobalInfo.CurrentUser.USERNAME;
                currentMessage.To = String.Join(",", users.Except(new List<string>() { GlobalInfo.CurrentUser.USERNAME }));
                currentMessage.CREATEUSER = GlobalInfo.CurrentUser.USERNAME;
                using (IBLLDealMessage bLLDealMessage = NinjectProvider.NinjectCore.GetService<IBLLDealMessage>())
                {
                    bLLDealMessage.DealMessage(currentMessage, CustomMessage == null ? Guid.Empty : CustomMessage.ID);
                    //下面的代码运行效率很低,后续需要优化，另,若需要对话,而不是处理系统的消息的情况,请使用其他的通讯工具
                    foreach (var item in messages.Where(m =>
                    {
                        return m.ReadUser.Split(',').All(t => t.Contains(GlobalInfo.CurrentUser.USERNAME));
                    }))
                    {
                        bLLDealMessage.DealMessage(null, item.ID);
                    }
                }
                StringBuilder sb = new StringBuilder();
                sb.Append(GlobalInfo.CurrentUser.USERNAME + ":\n");
                sb.Append(richDealMessage.Text + "\n");
                richMessage.Text += sb.ToString();
                richDealMessage.Text = "";
            }
        }

        private void richDealMessage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnDeal_Click(null, null);
            }
        }
    }
}
